<?php

if (!defined('IN_SEXMALL'))
{
	die('Hacking attempt');
}


/**
 * 调用会员信息
 *
 * @access  public
 * @return  string
 */
function insert_member_info($args=array())
{
	global $mInterface;

	$path = empty($args['path']) ? 'library' : $args['path'];
	$file = empty($args['file']) ? 'member_info.lbi' : $args['file'];

	$need_cache = $GLOBALS['smarty']->caching;
	$GLOBALS['smarty']->caching = false;

	$user_token = !empty($_GET['usertoken']) ? trim($_GET['usertoken']) :get_user_token();
	$username = !empty($_GET['username']) ? trim($_GET['username']) : get_username();

	if ($username == '{usertoken}'){
		$_GET['username'] = '';
	}
	if ($user_token == '{usertoken}' || strlen($user_token) != 32){
		$user_token = null;
	}

	if ($user_token && $username)
	{
		$userInfo = get_user_info($user_token);	// 获取用户信息
		$user = $userInfo['info'];
		$user['username'] = $username;

		$cart = get_cart($user_token, false);	// 获取购物袋

		$user['cart_count'] = intval($cart['goods_count']);
	}
	else if (!empty($username))
	{
		//$user['username'] = stripslashes($username);
	}
	else
	{
		$user = array();
	}

	$GLOBALS['smarty']->assign('user_info', $user);

	$output = $GLOBALS['smarty']->fetch($path . '/' . $file);

	$GLOBALS['smarty']->caching = $need_cache;

	return $output;
}


/**
 * 热搜关键字
 */
function insert_hotSearch($arr)
{
	global $mInterface,$db,$mem;

	$isTouch = $arr['isTouch'];

	$hotSearch = array();
	//$cache_name = 'sexmall_hotSearch'.$isTouch;
	//$hotSearch = $mem->getCache($cache_name, true);

	$hotSearch = read_static_cache('sexmall_hotSearch', 0, 0, true);

	// 获取热搜关键字
	if (empty($hotSearch) || !is_array($hotSearch))
	{
		if ($isTouch)
		{
			$sql = "SELECT ad_id, ad_name as hotKey, target " .
				" FROM ".TABLEPRE."ad WHERE position_id=4 AND media_type=3 AND enabled=1" .
				" ORDER BY rank ASC,ad_id DESC";
			$hotSearch = $db->getAll($sql);
			if(!$hotSearch)
			{
				$hotSearch = $mInterface->getHotSearchKeywords();
			}
		}
		else
		{
			$hotSearch = $mInterface->getHotSearchKeywords();
		}

		(empty($hotSearch) || !is_array($hotSearch)) && $hotSearch = array();
		write_static_cache('sexmall_hotSearch', $hotSearch, true);
	}

	$hotSearch_str = array();
	foreach ((array)$hotSearch AS $val){
		$hotSearch_str[] = '<a href="index.php?m=search&keywords='.$val['hotKey'].'">'.$val['hotKey'].'</a>';
	}

	if (!$isTouch)
	$hotSearch_str = implode('&nbsp;|&nbsp;', $hotSearch_str);
	else
	$hotSearch_str = implode('&nbsp;', $hotSearch_str);

	return $hotSearch_str;
}


/**
 * 购物袋信息
 */
function insert_cart_info()
{
	$need_cache = $GLOBALS['smarty']->caching;
	$GLOBALS['smarty']->caching = false;

	/* 购物袋信息 */
	$user_token = get_user_token();

	$now = local_time();

	if (!empty($user_token))
	{
		// 取得用户购物袋，数量、金额统计
		$cart_info = get_cart($user_token, false);

		$cart = array('count'=>$cart_info['goods_count'], 'amount'=>$cart_info['orders_total'], 'clear_time'=>'');

		$shop_time = 0;
		if (!empty($cart_info) && is_array($cart_info))
		{
			foreach ($cart_info['orders_detail'] AS $key => $val)
			{
				foreach ($val['order_goods'] AS $k => $v){
					// 最近加入购物袋时间
					if ($v['add_time'] > $shop_time){
						$shop_time = $v['add_time'];
					}
				}
			}
		}


		// 购物袋为空
		if ($cart['count'] < 1)
		{
			$GLOBALS['smarty']->assign('cart_unvisable', 	1);
			wap_setcookie("WAP[shop_time]");
		}
		else
		{
			$cart['clear_time'] = CART_EXPIRE - ($now - $shop_time);
			$cart['clear_time'] > CART_EXPIRE && $cart['clear_time'] = CART_EXPIRE;

			if ($cart['clear_time'] > 0)
			{
				$cart['formated_clear_time'] = floor($cart['clear_time'] / 60).'分'.($cart['clear_time'] % 60).'秒';
			}

			$GLOBALS['smarty']->assign('cart', 	$cart);
		}
	}
	else
	{
		// 未登录不显示购物袋
		$GLOBALS['smarty']->assign('cart_unvisable', 	1);
	}

	$output = $GLOBALS['smarty']->fetch('public/footer_cart_info.html');

	$GLOBALS['smarty']->caching = $need_cache;

	return $output;
}


?>